import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.PriorityQueue;

/**
 * implements an iterator that iterate a specified collection in ascending order.
 * @author BaoTam
 *
 * @param <T>
 */
public class AscendingIterator<T extends Comparable<T>> implements Iterator<T> {
	PriorityQueue<T> pQueue;
	
	/**
	 * constructs an iterator that iterates through the specified collection
	 * @param items
	 */
	public AscendingIterator(Collection<T> items) {
		pQueue = (items == null) ? new PriorityQueue<T>() : new PriorityQueue<T>(items);
	}

	/**
	 * @return true if there is a next element, false otherwise
	 */
	public boolean hasNext() {
		return pQueue.peek() != null;
	}

	/**
	 * go to and return the next element
	 * @return the next element
	 */
	public T next() {
		return pQueue.poll();
	}

	/**
	 * unsupported
	 */
	public void remove() {
		throw new UnsupportedOperationException();
	}
	
	/**
	 * main method for debugging purpose
	 * @param args
	 */
	public static void main(String[] args) {
		Collection<Integer> collection = new ArrayList<Integer>();
		Integer[] intArr = {1,2,3,3,5,7,9,11,6,8,8,10,9,12};
		for(Integer i : intArr) {
			collection.add(i);
		}
		AscendingIterator<Integer> ai = new AscendingIterator<Integer>(collection);
		System.out.println(ai.pQueue);
	}
}
